#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#define SIZE 1024
#define SEP " "
#define NUM 64

char* argv[NUM];
char buffer[SIZE];
char* GetEnv(const char* env)
{
    char* str = getenv(env);
    if(str == NULL)
    {
        printf("no match to the env!\n");
        exit(2);
    }
    return str;
}
void Interactive()
{
    printf("[%s@%s %s]$ ",GetEnv("USER"),GetEnv("HOSTNAME"),GetEnv("PWD"));
    fgets(buffer,SIZE,stdin);
    buffer[strlen(buffer)-1] = 0;
}
int BulitInCom()
{
    if(strcmp("cd",argv[0]) == 0)
    {
        if(argv[1] == NULL)
        {
            chdir("~");
        }
        else 
        {
            chdir(argv[1]);
            char pwd[1024];
            getcwd(pwd,1024);
            exec
        }
    }
}
int main()
{
    while(1)
    {
         //shell与用户交互
         Interactive();
         //字符串切割
         int i = 0;
         argv[i++] = strtok(buffer," ");
         while(argv[i++] = strtok(NULL," "));
         //处理内建命令
         
         //程序执行
         pid_t id = fork();
         if(id == 0)
         {
             //子进程
             execvp(argv[0],argv);
             exit(1);
             printf("execvp fail!\n");
         }
         pid_t rid = waitpid(id,NULL,0);
    }
    return 0;
}
